import { defineStore } from "pinia";
import router from "@/router";

export const DeindPinia = defineStore("main", {
  state:()=>{
    return {
      team:[
        {
          name: "home",
          path: "/index/home",
          query: {},
          isClose: false,
          meta: { title: "home" },
        }
      ]
    }
  },
  getters: {
    team(state) {
      return state.team;
    },
    // 监听面包屑和添加的标签
    getRoute(state: any, route: any) {
      const { name, path, query, meta, fullpath, params } = route; //解构数据
      if (route.matched.length >= 2 && name != "refres") {
        let findTag = state.team.find((p: any) => p.path == path);
        let index = state.team.findIndex(
          (item: any) => item.name == "productlist"
        ); //第一次是-1
        if (!findTag) {
          let ername = router.currentRoute.value.name; //名称
          if (ername == "producsetup" || ername == "producamend") {
            //等于就加入
            let aud = index > -1 ? index + 1 : state.team.length;
            state.team.splice(aud, 0, {
              name,
              path,
              query,
              fullpath,
              params,
              isClose: true,
              meta,
            });
          } else {
            state.team.push({
              name,
              path,
              query,
              fullpath,
              params,
              isClose: true,
              meta,
            });
          }
        }
      }
    },
    //选中路由
    selectRouter(state: any, path: any) {
      router.push(path);
    },

    // 删除标签
    deleteatap(state: any, index: any) {
      if (router.currentRoute.value.path == state.team[index].path) {
        const name = (state.team[index + 1] || state.team[index - 1]).path;
        router.push(name);
      }
      state.team.splice(index, 1);
    },
    // 路由器删除
    Routerdelete(state: any, path: any) {
      let index = state.team.findIndex((item: any) => item.path === path);
      if (-1 != index) {
        dispatch("handleClose", index);
      }
    },
    // 刷新
    Refresh(state: any, path: any) {
      router.push("/index/refres");
      setTimeout(() => {
        router.push(path);
      }, 100);
    },

    // 关闭右边的标签
    Closeright(state: any, index: any) {
      let openTags = state.team;
      let activeIndex = openTags.findIndex(
        (item: any) => item.path === router.currentRoute.value.path
      );
      if (activeIndex > index) {
        state.team.splice(index + 1);
        router.push(state.team[index].path);
      }
    },

    // 全部关闭
    closeAll(state: any, value: any) {
      router.push({ path: state.team[0].path });
      state.team.splice(1, state.team.length - 1);
    },
  },
  actions:{
    // 监听面包屑和添加的标签
    getRoute(context: any, route: any) {
      context.commit("getRoute", route);
    },
    // 选中路由
    selectRouter(context: any, path: any) {
      context.commit("selectRoute", path);
    },
    // 删除标签
    deleteatap(context: any, index: any) {
      context.commit("deleteatap", index);
    },
    // 路由器删除
    Routerdelete(context: any, path: any) {
      context.commit("Routerdelete", path);
    },
    // 刷新
    Refresh(context: any, path: any) {
      context.commit("Refresh", path);
    },
    // 关闭右边所有标签
    Closeright(context: any, index: any) {
      context.commit("Closeright", index);
    },
    // 全部关闭
    closeAll(context: any, value: any) {
      context.commit("closeAll", value);
    },
  },
});


function dispatch(arg0: string, index: any) {
  throw new Error("Function not implemented.");
}
